#ifndef __L3_LIB_DOWNLOAD_H__
#define __L3_LIB_DOWNLOAD_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "global.h"

/* 获取数组大小 */
#define SIZEOF_ARRAY(a)  (sizeof(a) / sizeof((a)[0]))

/* 错误码定义 */
#define   PARSE_DOWNLOAD_PATTERN_INFO_SUCCESS       0 
#define   DOWNLOAD_PATTERN_SEND_REQUEST_SUCCESS     1
#define   DOWNLOAD_PATTERN_RESPONSE_HEADER_SUCCESS  2
#define   DOWNLOAD_PATTERN_BODY_SUCCESS             3
#define   DOWNLOAD_PATTERN_CHECK_MD5_SUCCESS        4
#define   RELOAD_NEW_PATTERN_SUCCESS                5
#define   ROLLBACK_OLD_PATTERN_SUCCESS              6


#define   PARSE_DOWNLOAD_PATTERN_INFO_ERROR        -2
#define   DOWNLOAD_PATTERN_SEND_REQUEST_FAIL       -3   
#define   DOWNLOAD_PATTERN_RESPONSE_HEADER_FAILE   -4
#define   DOWNLOAD_PATTERN_BODY_FAILE              -5
#define   GET_DOWNLOAD_PATTERN_FILE_MD5_FAIL       -6
#define   DOWNLOAD_PATTERN_CHECK_MD5_FAIL          -7
#define   RELOAD_NEW_PATTERN_FAIL                  -8
#define   RM_NEW_PATTERN_FILE_FAIL                 -9
#define   ROLLBACK_OLD_PATTERN_FAIL                -10


/* smartX模块返回值定义 */
#define  SMARTX_SUCCESS                         0
#define  SMARTX_FAILURE                        -1

#define  SMARTX_INPUT_NULL                           0x01
#define  SMARTX_EVENT_NOT_EXIST                      0x02
#define  SMARTX_EVENT_EVENT_ID_ERROR                 0x03
#define  SMARTX_INVALID_GEARS                        0x04

#define  SMARTX_LIB_NOT_EXIST                        0x05
#define  SMARTX_MEM_ALLOC_FAIL                       0x06
#define  SMARTX_LIB_FILE_PROCESS_FAIL                0x07
#define  SMARTX_LIB_FILE_PARSE_FAIL                  0x08
#define  SMARTX_PARSE_PATTERN_INFO_FAIL              0x09
#define  SMARTX_LIB_CHECK_LEN_ERROR                  0x0a
#define  SMARTX_OPEN_LIB_FAIL                        0x0b
#define  SMARTX_INDICATORS_MATCH_FAIL                0x0c
#define  SMARTX_INDICATORS_MATCH_SUCCESS             0x0d
#define  SMARTX_INDICATORS_VALUE_MATCH_FAIL          0x0e
#define  SMARTX_INDICATORS_VALUE_MATCH_SUCCESS       0x0f
#define  SMARTX_PATTERN_MATCH_FAIL                   0x10
#define  SMARTX_PATTERN_MATCH_SUCCESS                0x11
#define  SMARTX_PATTERN_CRC_CHECK_FAIL               0x12

#define  SMARTX_PATTERN_MALLOC_DYNAMIC_RULE_POOL_FAIL 0x13
#define  SMARTX_PATTERN_MALLOC_DYNAMIC_RULE_BUF_FAIL  0x14

#define MD5_STR_LEN      (32)
#define MD5_VAL_HEX_LEN  (16)
#define L3_PATTERN_URL_LEN     256

#define  MAX_L3_ACTION_CNT      16

#define  MAX_L3_INDICATION_CNT  16


#define  MAX_EXEC_ACTION_NUM     16

#define  MAX_MATCH_ACTION_CNT    8


#define  MAX_PATTERN_VERSION_LEN  128 


/* 模式中指标和动作的值类型,当前用到的是INT32_VAL和UINT32_VAL*/
typedef enum
{
    RESERVED_VAL  = 0x00,
    INT32_VAL     = 0x01,
    UINT32_VAL    = 0x02,
    STRING_VAL    = 0x03,
    OBJECT_VAL    = 0x04,
}l3_value_type;

/* 指标类型 */
typedef  enum
{
    PITCH_ANGLE   = 0x01,    
    ROLL_ANGLE    = 0x02,   
    GYRO_X_VAL    = 0x03,   
    GYRO_Y_VAL    = 0x04,   
    AUTO_GRAR_VAL = 0x05,     
}l3_indicator_type;

/* 动作类型 */
typedef  enum
{
    ADJUST_GEAR_VAlUE        = 0x01, 
    ADJUST_GEAR_BASED_AUTO   = 0x02,  
}l3_action_type;

/* 模式库下载中间状态信息 */
typedef struct
{
    char  is_processing;
    int   received_len;  
    int   total_len;
    char  md5[MD5_STR_LEN + 1];
    int   url_len;
    char  url[L3_PATTERN_URL_LEN + 1];    
}l3_lib_download_ctx;

/* 模式库头部信息 */
typedef  struct
{
    uint16_t    start_flag;              /* 开始标记固定值为0xABAB */
    uint16_t    content_len;             /* 模式库内容长度 */
    uint16_t    head_len;                
    uint8_t     pattern_type;          
    uint8_t     *p_car_type;             
    uint16_t    pattern_version_id;      
    char        *p_pattern_version;      
    uint32_t    published_time;         
    uint16_t    pattern_cnt;             
    uint16_t    crc;                     
}l3_pattern_header_st;

/* 指标 */
typedef struct l3_indicators_st
{
    uint16_t    indicator_len;
    uint8_t     indicator_id;
    uint8_t     indicator_type;
    uint8_t     match_type;
    uint8_t     vale_type;
    uint8_t     value_num;
    uint32_t    value[MAX_L3_INDICATION_CNT];
    struct      l3_indicators_st* p_l3_indicators_next;
}l3_indicators_st;


/* 动作 */
typedef struct l3_action_st
{
     uint16_t    action_len; 
     uint8_t     action_id;
     uint8_t     action_type;
     uint8_t     action_para_type;
     uint8_t     action_para_num;
     uint32_t    action_para_value[MAX_L3_ACTION_CNT];
     struct      l3_action_st* p_l3_action_next;
}l3_action_st;


/* 模式 */
typedef struct l3_pattern_content_st
{
     uint16_t          pattern_content_len;
     uint32_t          pattern_id;
     uint8_t           priority;
     uint16_t          indicators_cnt;
     uint16_t          action_cnt;
     l3_action_st      *p_l3_action;
     l3_indicators_st  *p_l3_indicators;
     struct     l3_pattern_content_st* p_l3_pattern_next;
}l3_pattern_content_st;

typedef struct 
{
    uint8_t*   pool; 
    uint32_t   size;
    uint32_t   offset;
}l3_lib_pool;

int swei_l3_lib_module_init(void);

int swei_l3_lib_load_from_flash(void);

void tbox_l3_deinit_lib(void);

#endif

